package com.weiqiao.mall.repository.impl;

import com.google.gson.Gson;
import com.mingsoft.util.StringUtil;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.FindOneAndUpdateOptions;
import com.weiqiao.base.dao.impl.BaseMongoDaoImpl;
import com.weiqiao.mall.domain.AppUserEntity;
import com.weiqiao.mall.repository.IAppUserRepository;
import org.apache.commons.collections4.CollectionUtils;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository("AppUserRepositoryImpl")
public class AppUserRepositoryImpl extends BaseMongoDaoImpl implements IAppUserRepository {
    private final static String collectionName = "app_user_ext_";

    @Override
    public boolean addUserEntity(AppUserEntity entity, int company_id) {
        Bson filter = null;
        if (!StringUtil.isBlank(entity.getUnionid())) {
            filter = Filters.eq("unionid", entity.getUnionid());
        }
        if (!StringUtil.isBlank(entity.getPhone())) {
            filter = Filters.eq("phone", entity.getPhone());
        }
        if (StringUtil.isBlank(filter)) {
            return false;
        }
        Document doc = Document.parse(new Gson().toJson(entity));
        return this.findOneAndUpate(company_id, filter, doc);
    }

    @Override
    public boolean updateEntity(int company_id, Bson filter, Document doc, boolean isAdd) {
        return super.updateOne(collectionName + company_id, filter, doc, isAdd);
    }

    @Override
    public String getAppUserByFilter(Bson filter, int company_id) {
        return this.findOne(collectionName + company_id, filter);
    }

    @Override
    public boolean updateAppUser(Document doc, Bson filter, int company_id) {
        return this.updateOne(collectionName + company_id, filter, doc, false);
    }

    @Override
    public List<String> getAppUserListByUserId(Bson filter, int company_id) {
        return this.findFilter(collectionName + company_id, filter);
    }

    @Override
    public List<String> getAppUserListByUserId(Bson filter, int company_id, Bson sort) {
        return this.findFilterSort(collectionName + company_id, filter, sort);
    }

    @Override
    public List<String> getAppUserByFilterSortSkipLimit(Bson filter, Bson sort, int skip, int limit, int company_id) {
        return this.findFilterSortSkipLimit(collectionName + company_id, filter, sort, skip, limit);
    }

    /**
     * 查询满足条件的个数
     *
     * @param filter 查询条件
     * @return int
     */
    @Override
    public int countFruiter(int company_id, Bson filter) {
        return this.statististics(collectionName + company_id, filter);
    }

    @Override
    public boolean findOneAndUpate(int companyid, Bson filters, Document doc) {
        MongoCollection<Document> dbCollection = getCollection(collectionName + companyid);
        dbCollection.findOneAndUpdate(filters, new Document("$set", doc), new FindOneAndUpdateOptions().upsert(true));
        return false;
    }

    @Override
    public boolean updateManyByIds(Document doc, Bson filter, int company_id) {
        return this.updateMany(collectionName + company_id, filter, doc, false);
    }

    @Override
    public boolean updateMany(Document doc, Bson filter, int company_id) {
        return this.updateMany(collectionName + company_id, filter, doc, false);
    }

    @Override
    public void updateOne(int company_id, Bson filter, String s) {
        this.updateReplace(collectionName + company_id, filter, s);
    }

    @Override
    public boolean deleteOne(int companyid, int user_id) {
        Bson filter = Filters.eq("user_id", user_id);
        this.deleteOne(collectionName + companyid, filter);
        return true;
    }

    @Override
    public List<String> getUserPage(int company_id, Bson filter, Pageable pageable) {
        return this.getListAndPage(collectionName + company_id, filter, pageable);
    }

    @Override
    public boolean updateAppUser(String json, Bson filter, int company_id) {
        return super.updateOne(collectionName + company_id, filter, json, false);
    }

    @Override
    public List<AppUserEntity> getListAndProject(int companyId, Bson filter, Document project) {
        List<AppUserEntity> result = new ArrayList<>();
        List<String> list = super.findToProject(collectionName + companyId, filter, project);
        if (CollectionUtils.isNotEmpty(list)) {
            Gson gson = new Gson();
            list.forEach(info -> {
                AppUserEntity entity = gson.fromJson(info, AppUserEntity.class);
                result.add(entity);
            });
        }
        return result;
    }
}
